// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var router = require("../hash/pageRouter");
	var bulletinId = base.getUrlParam("bulletinId");
	var css_product = require("./constractLogisticsManage.css");
	var fileSrc = "";
	var constant = require("constant");
	var fileSize = "";
	var fileName = "";
	var paramName = "";
	var paramId = "";
	var paramValue = "";
	var paramType = "";
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		initEvent();
		inquireFinanceProductPageList();
		//		getParamList();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#addConstractBtn").on("click", function() {
			router.route("constractLogisticsManage/constractLogisticsAdd.html");
		});
		
		$(".published").unbind("click").click(function(){
			router.route("constractLogisticsManage/constractLogisticsManage.html");
		});
		$(".unExam").unbind("click").click(function(){
			router.route("constractLogisticsManage/constractLogisticsManageUnexam.html");
		});
		$(".unPublish").unbind("click").click(function(){
			router.route("constractLogisticsManage/constractLogisticsManageUnpublish.html");
		});
		$(".unSubmit").unbind("click").click(function(){
			router.route("constractLogisticsManage/constractLogisticsManageUnsubmit.html.html");
		});
		$(".stoped").unbind("click").click(function() {
			router.route("constractLogisticsManage/constractLogisticsManageStoped.html");
		});
		
		inquireCurrentOperationList();
	};
	
	//权限控制
	var inquireCurrentOperationList = function() {
		base.fnAjax({
			action: 'inquireCurrentOperationList.json',
			async: false,
			fnSuccess: function(data) {
				if (data.status == 200) {
					for (var i = 0; i < data.datas.length; i++) {
						var permission = data.datas[i];
						var permissionId = permission.permissionId;
						$.each($("#constractLogisticsManageUnpublish .permissionLink"), function() {
							var $this = $(this);
							if ($this.attr("permissionId") == permissionId) {
								$this.removeClass("disabled");
								$this.removeClass("none");
							}
						});
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	//查询产品列表
	var inquireFinanceProductPageList = function() {
		base.fnAjax({
			action: "inquireFinanceProductPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				productScope: 2,
				state: 3
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#constractManageUnpublishListTpl").html();
					var html = juicer(tpl, data);
					$("#constractManageUnpublishListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#contractUnpublishPage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceProductPageList();
						});
					$(".productPublish").click(function() {
						var pId = $(this).parents("tr").attr("productId");
						dialog.showDialog($("#confirmDialog"));
						dialog.showConfirmDialog("发布产品", "是否要发布该产品？", function() {
							publishFinanceProduct(pId);
						});
					});
					
					$(".productDetail").click(function() {
						var pId = $(this).parents("tr").attr("productId");
						router.route("constractLogisticsManage/constractLogisticsDetail.html?productId="+pId);
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//发布产品
	var publishFinanceProduct = function(productId) {
		base.fnAjax({
			action: "publishFinanceProduct.json",
			oData: {
				productId: productId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					init();
					base.alert(1, "发布成功！");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});